package com.qf.service.impl;

import com.qf.mapper.*;
import com.qf.model.*;
import com.qf.page.PageResult;
import com.qf.page.QueryPageBean;
import com.qf.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private StageMapper stageMapper;
    @Autowired
    private ContractorMapper contractorMapper;
    @Autowired
    private DeveloperMapper developerMapper;
    @Autowired
    private SubitemMapper subitemMapper;
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private BranchMapper branchMapper;
    @Autowired
    private TaskplanMapper taskplanMapper;

    @Override
    public List<Project> getAll() {
        List<Project> all = projectMapper.findAll();
        return all;
    }

    //分页查询+模糊查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer CurrentPage = queryPageBean.getCurrentPage();
        Integer PageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageResult pageResult = new PageResult();
        //当前页结果
        List<Project> rows = projectMapper.findPage((CurrentPage-1)*PageSize, PageSize,queryString);
        //总记录数
        Long total = projectMapper.count(queryString);

        pageResult.setTotal(total);
        pageResult.setRows(rows);

        return pageResult;
    }

    //新增project对象
    @Override
    @Transactional
    public int addProject(Project project) {
        project.setUpdatetime(new Date());
        int i = projectMapper.addOne(project);
        return i;
    }


    //根据pid查询project
    @Override
    public Project findById(Integer pid) {
        Project project = projectMapper.findById(pid);
        return project;
    }

    //修改project
    @Override
    @Transactional
    public int edit(Project project) {
        project.setUpdatetime(new Date());
        int i = projectMapper.editOne(project);
        return i;
    }

    //删除project
    @Override
    @Transactional
    public int delete(Integer pid) {
        int i = projectMapper.delete(pid);
        return i;
    }


    @Override
    public Map<String, Object> index(int pid) {
        ProAndDevCon proAndDevCon = projectMapper.index(pid);
//        Contractor contractor = contractorMapper.findByPid(pid);
//        Developer developer = developerMapper.findByPid(pid);
//        proAndDevCon.setCname(contractor.getCname());
//        proAndDevCon.setDname(developer.getDname());
        List<Stage> stages = stageMapper.findByPid(pid);
        Map<String,Object> map= new HashMap<>();
        map.put("proAndDevCon",proAndDevCon);
        map.put("stages",stages);

        return map;
    }

    @Override
    public List<Subitem> cname(int pid) {
        List<Subitem> subitems = subitemMapper.findByPid(pid);
        return subitems;
    }

    @Override
    public List<Item> item(String suid) {
        List<Item> items = itemMapper.findBySuid(suid);
        return items;
    }

    @Override
    public List<Branch> bname(int pid) {
        List<Branch> branches = branchMapper.findByPid(pid);
        return branches;
    }

    @Override
    public List<Taskplan> taskplan(String bid) {
        List<Taskplan> taskplans = taskplanMapper.findByBid(bid);
        return taskplans;
    }
}
